package com.xjm.api.web;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjm.entity.model.ClassifyModel;
import com.xjm.entity.model.ProductsModel;
import com.xjm.entity.vo.PageVo;
import com.xjm.entity.vo.Result;
import com.xjm.service.ClassifyService;
import com.xjm.service.IProductsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xjm
 * @since 2025-02-27
 */
@RestController
@RequestMapping("/user/product")
@RequiredArgsConstructor
public class WebProductsController {

    private final IProductsService productsService;
    private final ClassifyService classifyService;

    @GetMapping("/classify")
    public Result<List<ClassifyModel>> classify() {
        LambdaQueryWrapper<ClassifyModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClassifyModel::getStatus, 1);
        return Result.success(classifyService.list(wrapper));
    }

    @GetMapping
    public Result<PageVo<ProductsModel>> list(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "5") Long size,
            @RequestParam(required = false) String classifyId,
            @RequestParam(required = false) String priceRange,
            @RequestParam(required = false) String scenario
    ) {
        LambdaQueryWrapper<ProductsModel> wrapper = new LambdaQueryWrapper<>();
        BigDecimal minPrice = null;
        BigDecimal maxPrice = null;
        if (priceRange != null) {
            String[] prices = priceRange.split("-");
            if (prices.length == 2) {
                minPrice = new BigDecimal(prices[0]);
                maxPrice = new BigDecimal(prices[1]);
            }
        }
        wrapper.eq(ProductsModel::getStatus, 1);
        wrapper.eq(classifyId != null && !classifyId.equals("all"), ProductsModel::getClassifyId, classifyId);
        wrapper.ge(minPrice != null, ProductsModel::getPrice, minPrice);
        wrapper.le(maxPrice != null, ProductsModel::getPrice, maxPrice);
        wrapper.eq(scenario != null && !scenario.equals("all"), ProductsModel::getScenario, scenario);
        wrapper.orderByDesc(ProductsModel::getSales);
        Page<ProductsModel> pageResult = productsService.page(new Page<>(page, size), wrapper);
        pageResult.getRecords().forEach(item -> item.setOther(JSONUtil.parseArray(item.getOther())));
        return Result.success(pageResult);
    }

    @GetMapping("/hot")
    public Result<List<ProductsModel>> hot() {
        LambdaQueryWrapper<ProductsModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductsModel::getStatus, 1);
        wrapper.orderByDesc(ProductsModel::getSales);
        wrapper.last("limit 4");
        return Result.success(productsService.list(wrapper));
    }
}
